Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject RESPECT NULLS and IGNORE NULLS for aggregate functions #15014

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

qazxcdswe123
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

As per the issue says

In the SQL standard, RESPECT NULLS and IGNORE NULLS are options to be set for the lead, lag, first_value, last_value and nth_value window functions.

What changes are included in this PR?

Adds validation to prevent using RESPECT NULLS and IGNORE NULLS with aggregate functions

One thing to note that is:

That being said, part of the weirdness here is that DataFusion defines first_value both as an aggregate function and as a window function.

Accroding to the sqllogic test, this should only affect aggregate function.

Are these changes tested?

Essisting tests have coverd the cases

Are there any user-facing changes?

Maybe? If one uses RESPECT NULLS and IGNORE NULLS with aggregate functions then this is a breaking change

Adds validation to prevent using RESPECT NULLS and IGNORE NULLS with aggregate functions
@github-actions github-actions bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels Mar 5, 2025
@qazxcdswe123 qazxcdswe123 marked this pull request as draft March 5, 2025 04:37
@qazxcdswe123 qazxcdswe123 marked this pull request as ready for review March 5, 2025 04:39
@@ -5893,15 +5889,11 @@ SELECT FIRST_VALUE(column1 ORDER BY column2) FROM t;
----
NULL

query I
query error DataFusion error: Error during planning: RESPECT NULLS and IGNORE NULLS are not supported for aggregate functions
SELECT FIRST_VALUE(column1 ORDER BY column2) RESPECT NULLS FROM t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this change -- the query seems valid to me as there is a null and an argument order, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of the weirdness I pointed out in #15006, which is that DataFusion has first_value as both an aggregate and window function, whereas most other engines only have a window function form.

This invocation is technically the aggregate function form, because there is no OVER clause which would be expected for a window function invocation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree -- I think spark behaves this way which is why someone added the feature to DataFusion

Copy link
Contributor

@vbarua vbarua Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, there's always another engine 😅

It looks like Spark defines afirst_value(expr[, isIgnoreNull]) function which takes an optional boolean parameter to control whether it ignore nulls.

Spark Docs: https://spark.apache.org/docs/latest/api/sql/index.html#first_value

@@ -349,6 +349,12 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
} else {
// User defined aggregate functions (UDAF) have precedence in case it has the same name as a scalar built-in function
if let Some(fm) = self.context_provider.get_aggregate_meta(&name) {
// Reject RESPECT NULLS and IGNORE NULLS for aggregate functions
// See https://github.com/apache/datafusion/issues/15006
if null_treatment.is_some() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider it to be a parser responsibility, for example duckDB

D SELECT LAST_VALUE(column1) RESPECT NULLS FROM t
  ;
Parser Error: syntax error at or near "RESPECT"
LINE 1: SELECT LAST_VALUE(column1) RESPECT NULLS FROM t
                                   ^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I totally agree! Does this mean I should make change in https://github.com/apache/datafusion-sqlparser-rs ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think someone needs to go see what spark does too -- as I recall this was some feature from spark that someone added explicitly...

Maybe @andygrove or @huaxingao remembers 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants